package com.simon.app.october.common.aspect;

import com.simon.app.october.common.annotation.SysLog;
import com.simon.app.october.common.utils.HttpContextUtils;
import com.simon.app.october.common.utils.IPUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 一个切面，用来具体操作记录
 * */
@Aspect
@Component
public class SysLogAspect {

    /**
     * 定义一个切点
     * */
    @Pointcut("@annotation(com.simon.app.october.common.annotation.SysLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method          method    = signature.getMethod();
        long            startTime = System.currentTimeMillis();
        SysLog          sysLog    = method.getAnnotation(SysLog.class);

        Object result = point.proceed();

        // 获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        // 获取IP
        System.out.println(IPUtils.getIpAddr(request));

        return result;
    }
}
